/**
 * @param {number} x
 * @return {boolean}
 */

// TODO: complete this object/class

// The constructor takes in an array of items and a integer indicating how many
// items fit within a single page
function PaginationHelper(collection, itemsPerPage) {
  this.collection = collection
  this.itemsPerPage = itemsPerPage
  this.pagecount = Math.floor(this.collection.length / this.itemsPerPage) + 1
  this.itemcount = this.collection.size()
  // let items = JSON.stringify(collection).split(',').length
  // console.log(items);
}

// returns the number of items within the entire collection
PaginationHelper.prototype.itemCount = function () {
  return this.itemcount
}

// returns the number of pages
PaginationHelper.prototype.pageCount = function () {
  return this.pagecount
}

// returns the number of items on the current page. page_index is zero based.
// this method should return -1 for pageIndex values that are out of range
PaginationHelper.prototype.pageItemCount = function (pageIndex) {
  pageIndex++
  if (pageIndex < this.pagecount) return this.itemsPerPage
  else if (pageIndex == this.pagecount) return this.pagecount % this.itemsPerPage
  else return -1
}

// determines what page an item is on. Zero based indexes
// this method should return -1 for itemIndex values that are out of range
PaginationHelper.prototype.pageIndex = function (itemIndex) {
  if (itemIndex > this.itemcount || itemIndex < 0) return -1
  if (itemIndex == 0) return 0
  return Number.isInteger(itemIndex / this.itemsPerPage) ? itemIndex / this.itemsPerPage - 1 : Math.floor(itemIndex / this.itemsPerPage)
}




var helper = new PaginationHelper(['a', 'b', 'c', 'd', 'e', 'f'], 4);
// console.log(helper.pageCount());
// console.log(helper.itemCount());
// console.log(helper.pageItemCount(0));
// console.log(helper.pageItemCount(1));
// console.log(helper.pageItemCount(2));
console.log(helper.pageIndex(7));
console.log(helper.pageIndex(1));
console.log(helper.pageIndex(2));
console.log(helper.pageIndex(4));
console.log(helper.pageIndex(5));
console.log(helper.pageIndex(-4));
console.log(helper.pageIndex(0));

 //should == 2
 //should == 6
 //should == 4
 // last page - should == 2
 // should == -1 since the page is invalid